package kc.test.kmeans;

import java.awt.geom.Point2D;

public class HPoint implements Cloneable{
	protected double[] vec = null;

	public double distance(HPoint p){
		return Math.sqrt(distanceSq(p));
	}
	public double distanceSq(HPoint p){
		if (vec.length != p.getVec().length) {
			throw new IllegalArgumentException("point sizes don't match");
		}
		double dist = 0;
		for (int i=0; i<vec.length; i++){
			dist += (vec[i]-p.getVec()[i])*(vec[i]-p.getVec()[i]);
		}
		return dist;
	}
	public double getX(){
		return vec[0];
	}
	public double getY(){
		return vec[1];
	}
	public void setLocation(double...ds){
		if (vec == null || vec.length != ds.length) {
			vec = new double[ds.length];
		}
		System.arraycopy(ds, 0, vec, 0, ds.length);
	}
	public HPoint(double...ds){
		setLocation(ds);
	}
	
	public HPoint(Point2D p){
		setLocation(p.getX(),p.getY());
	}
	public double[] getVec(){
		return vec;
	}
	public int getDimCnt(){
		if (vec == null){
			return 0;
		}
		else{
			return vec.length;
		}
	}
	@Override
	protected Object clone() {
		HPoint p = new HPoint(this.vec);
		return p;
	}
}
